//
//  BrokCell.m
//  Cat
//
//  Created by lushitong on 2019/1/9.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "BrokCell.h"
#import "FLImageView.h"
#import "BrokerageView.h"
#import "BrokModel.h"

@interface BrokCell()

@property (nonatomic, strong) UIView *operView;
@property (nonatomic, strong) UILabel *createLabel;
@property (nonatomic, strong) FLImageView *detailsImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *orderLabel;
@property (nonatomic, strong) UIButton *copyButton;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) BrokerageView *brokeView;
@property (nonatomic, strong) UILabel *statusLabel;

@property (nonatomic, strong) BrokModel *infoModel;

@end

@implementation BrokCell

- (void)setCellModelData:(id)cellModelData {
	if ([cellModelData isKindOfClass:[BrokModel class]]) {
		BrokModel *model = (BrokModel *)cellModelData;
		_infoModel = model;
		self.createLabel.text = model.createTime;
		self.orderLabel.text = [NSString stringWithFormat:@"订单号：%@",model.orderNo];
		[self.detailsImageView setImageUrl:[NSURL URLWithString:model.imgUrl] size:CGSizeMake(kQKFitModuleDecimal(105), kQKFitModuleDecimal(105)) cornerRadius:0];
		self.brokeView.commissionNumber = model.prizeAmount;
		[self.brokeView mas_updateConstraints:^(MASConstraintMaker *make) {
			make.size.equalTo([BrokerageView getSizeWidthPrizeAmount:model.prizeAmount]);
		}];
		
		self.moneyLabel.text = [NSString stringWithFormat:@"￥%.2f",model.orderTotal.floatValue];
		self.titleLabel.text = model.title;
		self.statusLabel.text = [self changeStatus:model.status];
	}
}

- (NSString *)changeStatus:(NSString *)status {
	if ([status isEqualToString:@"1"]) {
		return @"待付款";
	} else if ([status isEqualToString:@"5"]) {
		return @"已付款";
	} else if ([status isEqualToString:@"9"]){
		return @"已结算";
	} else if ([status isEqualToString:@"0"]) {
		return @"失效";
	} else {
		return @"未知状态";
	}
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleNone;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		self.backgroundColor = COLOR_WITH_HEX(0xFAFAFA);
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	[self.contentView addSubview:self.operView];
	[self.operView addSubviews:@[self.createLabel,self.detailsImageView,self.titleLabel,self.orderLabel,self.copyButton,self.moneyLabel,self.brokeView,self.statusLabel]];
	
	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.contentView.mas_left).offset(kQKFitModuleDecimal(12));
		make.right.equalTo(self.contentView.mas_right).offset(-kQKFitModuleDecimal(12));
		make.top.equalTo(self.contentView.mas_top).offset(kQKFitModuleDecimal(20));
		make.bottom.equalTo(self.contentView.mas_bottom).offset(-kQKFitModuleDecimal(1.0));
	}];
	
	[self.createLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(14.0));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(9.0));
	}];
	
	[self.detailsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(105), kQKFitModuleDecimal(105)));
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(13.0));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(29));
	}];
	
	[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.detailsImageView.mas_right).offset(kQKFitModuleDecimal(12.0));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(26.0));
		make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(6.0));
	}];
	
	[self.orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.titleLabel.mas_left);
		make.top.equalTo(self.titleLabel.mas_bottom).offset(kQKFitModuleDecimal(6.0));
		make.right.lessThanOrEqualTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(55.0)).priorityHigh();
	}];
	
	[self.copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerY.equalTo(self.orderLabel.mas_centerY);
		make.left.equalTo(self.orderLabel.mas_right).offset(kQKFitModuleDecimal(6.0));
	}];
	
	[self.brokeView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.titleLabel.mas_left);
		make.bottom.equalTo(self.operView.mas_bottom).offset(-kQKFitModuleDecimal(13.0));
	}];
	
	[self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.titleLabel.mas_left);
		make.top.equalTo(self.orderLabel.mas_bottom).offset(kQKFitModuleDecimal(5.0));
	}];
	
	[self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(12.0));
		make.centerY.equalTo(self.brokeView.mas_centerY);
	}];
}

- (void)copyAction {
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
	pasteboard.string = self.infoModel.orderNo;
	[FLProgressHUD showSuccess:@"复制成功"];
}

#pragma mark----- lazy init -----

-(UIView *)operView {
	if (!_operView) {
		_operView = [[UIView alloc] init];
		_operView.backgroundColor = [UIColor whiteColor];
		_operView.layer.shadowColor = COLOR_WITH_HEX_A(0x000000, 0.2).CGColor;
		_operView.layer.shadowOffset = CGSizeZero;
		_operView.layer.shadowOpacity = 0.3; // 阴影透明度，默认0
		_operView.layer.shadowRadius = kQKFitModuleDecimal(2);    // 阴影半径，默认3
		_operView.layer.cornerRadius = kQKFitModuleDecimal(3);
	}
	return _operView;
}

- (UILabel *)createLabel {
	if (!_createLabel) {
		_createLabel = [UILabel labelWithText:@"创建日期 2018 09 10 19：24" font:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
	}
	return _createLabel;
}

- (FLImageView *)detailsImageView {
	if (!_detailsImageView) {
		_detailsImageView = [[FLImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, kQKFitModuleDecimal(105), kQKFitModuleDecimal(105))];
	}
	return _detailsImageView;
}

- (UILabel *)titleLabel {
	if (!_titleLabel) {
		_titleLabel = [UILabel labelWithText:@"淘宝宽口径新生儿PPSU奶瓶婴儿奶瓶宝宝" font:[UIFont boldSystemFontOfSize:kQKFitFont(14)] textColor:COLOR_WITH_HEX(0x4C4947)];
		_titleLabel.numberOfLines = 2;
	}
	return _titleLabel;
}

- (UILabel *)orderLabel {
	if (!_orderLabel) {
		_orderLabel = [UILabel labelWithText:@"订单号:798527464123467" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0x756D68)];
	}
	return _orderLabel;
}

- (UIButton *)copyButton {
	if (!_copyButton) {
		_copyButton = [UIButton buttonWithTitle:@"【复制】" color:COLOR_WITH_HEX(0xFFC236) font:[UIFont systemFontOfSize:kQKFitFont(12)]];
		[_copyButton addTarget:self action:@selector(copyAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _copyButton;
}

- (UILabel *)moneyLabel {
	if (!_moneyLabel) {
		_moneyLabel = [UILabel labelWithText:@"￥399.09" font:[UIFont systemFontOfSize:kQKFitFont(16)] textColor:COLOR_WITH_HEX(0xFF3F3F)];
	}
	return _moneyLabel;
}

- (BrokerageView *)brokeView {
	if (!_brokeView) {
		_brokeView = [[BrokerageView alloc] init];
		_brokeView.backgroundColor = COLOR_WITH_HEX(0x4C4947);
	}
	return _brokeView;
}

- (UILabel *)statusLabel {
	if (!_statusLabel) {
		_statusLabel = [UILabel labelWithText:@"已付款" font:[UIFont systemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0x4C4947)];
	}
	return _statusLabel;
}
@end

